﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using System.IO.IsolatedStorage;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.SystemViews
{
    public partial class LoginControl : BaseUserControl
    {
        SystemServiceReference.SystemServiceClient sysClient;
        private IsolatedStorageSettings appSettings = null;
      
        public LoginControl()
        {
            InitializeComponent();
            sysClient = new SystemServiceReference.SystemServiceClient();
            appSettings = IsolatedStorageSettings.ApplicationSettings;
            InitEvent();
        }

        public void InitEvent()
        {
            sysClient.GetEmployeeByNameAndPwdCompleted += new EventHandler<SystemServiceReference.GetEmployeeByNameAndPwdCompletedEventArgs>(sysClient_GetEmployeeByNameAndPwdCompleted);
            hbLogin.Click += new RoutedEventHandler(btnOk_Click);
            rbZ.Checked += new RoutedEventHandler(rbZ_Checked);
            rbY.Checked += new RoutedEventHandler(rbZ_Checked);

            //btnOk.Click += new RoutedEventHandler(btnOk_Click);
            //ckRLoginPwd.Checked += new RoutedEventHandler(ckRLoginPwd_Checked);
            //ckRLoginPwd.Unchecked += new RoutedEventHandler(ckRLoginPwd_Checked);

        }

        public void LoadData()
        {
            if (appSettings.Contains(SessionManager.ISCHINESE))
            {
                var cul = bool.Parse(appSettings[SessionManager.ISCHINESE].ToString());
                if (cul)
                    rbZ.IsChecked = true;
                else rbY.IsChecked = true;
            }
            if (appSettings.Contains(SessionManager.USERNAME))
            {
                tbLoginName.Text = appSettings[SessionManager.USERNAME].ToString();
                //ckRLoginName.IsChecked = true;
            }
            if (appSettings.Contains(SessionManager.PASSWORD))
            {
                tbLoginPwd.Password = appSettings[SessionManager.PASSWORD].ToString();
                //ckRLoginPwd.IsChecked = true;
            }
            //if (appSettings.Contains(SessionManager.SAVELOGINSTATE))
            //{
            //    ckRMdl.IsChecked = true;
            //}
        }

        void rbZ_Checked(object sender, RoutedEventArgs e)
        {
            string lanV = (sender as RadioButton).Tag.ToString();
            var strings = App.Current.Resources["lnCon"] as LanguageConverter;
            strings.ChangeCulture(lanV);            
        }

        void ckRLoginPwd_Checked(object sender, RoutedEventArgs e)
        {
            //if (ckRLoginPwd.IsChecked == true)
            //{
            //    ckRLoginName.IsChecked = true;
            //    ckRLoginName.IsEnabled = false;
            //}
            //else
            //{
            //    ckRLoginName.IsEnabled = true;
            //}
        }

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tbLoginName.Text))
            {
                errorMsg.Text = MyStrings.yhmbnwk;// "用户名不能为空";
                return;
            }
            if (string.IsNullOrEmpty(tbLoginPwd.Password))
            {
                errorMsg.Text = MyStrings.mmbnwk;// "密码不能为空";
                return;
            }
            errorMsg.Text = "";
            sysClient.GetEmployeeByNameAndPwdAsync(tbLoginName.Text, tbLoginPwd.Password);
        }

        void sysClient_GetEmployeeByNameAndPwdCompleted(object sender, SystemServiceReference.GetEmployeeByNameAndPwdCompletedEventArgs e)
        {
            if (e.Result == null || string.IsNullOrEmpty(e.Result.LoginName))
            {
                errorMsg.Text = MyStrings.yhmhmmcw;// "用户名或密码错误";
            }
            else
            {
                var cuUser = e.Result;
                //if (ckRLoginName.IsChecked == true)
                //    appSettings[SessionManager.USERNAME] = cuUser.LoginName;
                //else
                //{
                //    if (appSettings.Contains(SessionManager.USERNAME))
                //        appSettings.Remove(SessionManager.USERNAME);
                //}

                //if (ckRLoginPwd.IsChecked == true)
                //appSettings[SessionManager.PASSWORD] = cuUser.LoginPwd;
                //else
                //{
                //    if (appSettings.Contains(SessionManager.PASSWORD))
                //        appSettings.Remove(SessionManager.PASSWORD);
                //}

                //if (ckRMdl.IsChecked == true)
                //    appSettings[SessionManager.SAVELOGINSTATE] = true;
                //else
                //{
                //    if (appSettings.Contains(SessionManager.SAVELOGINSTATE))
                //        appSettings.Remove(SessionManager.SAVELOGINSTATE);
                //}
                appSettings[SessionManager.USERNAME] = cuUser.LoginName;
                appSettings[SessionManager.PASSWORD] = cuUser.LoginPwd;
                appSettings[SessionManager.SAVELOGINSTATE] = true;

                appSettings[SessionManager.USERINFO] = e.Result;
                (this.Tag as MainPage).LoadPage();
            }
        }

    }
}
